﻿using System;
using System.Globalization;
using System.Windows.Data;

namespace MuddyBoots.GreenlightQC.Mobile.UI.View.Converters
{
    public sealed class NumberConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var doubleValue = value as double?;
            if (doubleValue == null)
            {
                return null;
            }

            return doubleValue.Value.ToString(culture);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
            {
                return null;
            }

            var stringValue = value.ToString();

            return (string.IsNullOrWhiteSpace(stringValue) || stringValue == "+" || stringValue == "-")
                       ? (object)null
                       : double.Parse(stringValue, NumberStyles.Any, culture);
        }
    }
}
